Shell Class
Used to execute Unix or DOS shell commands under Windows, Mac OS X, or Linux.
More information available in parent classes: Object
Notes
Use the Shell class to execute DOS or Unix commands and get the results. It has a single method, Execute, which executes a one-line command in Synchronous mode. This causes two properties of the Shell object to change: ErrorCode, which is a system-supplied error code or 0 for no error; and Result, which is a String containing the output of the command. The TimeOut property specifies how long (in milliseconds) a process can run before it is automatically terminated. A value of -1 means the process can run indefinitely. This property currently applies only to Windows.
Examples
Using the synchronous mode, the following code lists the current directory's files (Mac OS X or Linux).
s= New Shell
#if TargetWin32
s.execute "dir"
#elseif ( TargetMacOS or TargetLinux) and Not( TargetMacOSClassic)
s.execute "ls -la"
#else
MsgBox "This doesn't run on Mac OS Classic!"
#endif
If s.errorCode = 0 then
EditField1.text = s.result
else
MsgBox "Error code: " + Str(s.errorCode)
end if
The following example gets the names and values of the environment variables on the user's computer and displays the information in an EditField. You can use the EnvironnmentVariable method of the System object to get or set individual environment variables.
Dim cmd as String
#if TargetMacOS or TargetLinux and Not( TargetMacOSClassic)
cmd="env"
#elseif TargetWin32
cmd="set"
#endif
s.execute cmd
if s.errorCode=0 then
EditField1.text=s.Result
else
EditField1.text="Error "+ Str(s.ErrorCode)
end if
The following terminal application allows you to submit Unix commands using the interactive mode. The interface consists of two EditFields, InputField, in which the user can enter a command, and OutputField that displays the results.
The Open event for the window initializes the shell object (declared as a property of the window).
The user can type a unix command into the EditField. When he presses Return, the following code in the EditField's KeyDown event runs. The Write method sends the command to the Shell's input buffer.
If Not mShell.IsRunning Then
mShell.Execute "sh"
End If
mShell.Write InputField.Text
mShell.Write Chr(13)
InputField.Text = ""
Return True
Else
Return False
End If
'
A Timer calls the ReadAll method in its Action event:
See Also
TargetCarbon, TargetWin32, TargetMacOSClassic, TargetMachO, TargetMacOS, TargetLinux constants.